package com.zuimei.zuimei.adapters;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.zuimei.zuimei.R;
import com.zuimei.zuimei.beans.CategoryFormatBean;

import java.util.List;

/**
 * Created by 任聪 on 2017/2/3.
 */
public class CategoryAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private List<CategoryFormatBean> list1;
    private Context context;
    private OnCategoryItemClicklistener onItemClickListener;
    public CategoryAdapter(Context context,List<CategoryFormatBean> list1){
        this.list1=list1;
        this.context=context;
    }
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if(viewType==1){
            return new MyViewHolder(LayoutInflater.from(context).inflate(R.layout.category_home_list,parent,false));
        }else if(viewType==2){
           return new MyViewHolder2(LayoutInflater.from(context).inflate(R.layout.category_listview_item,parent,false));
        }else {
            return  null;
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if(holder instanceof MyViewHolder){
            ((MyViewHolder) holder).tv.setText(list1.get(position).getTitle());
            Glide.with(context.getApplicationContext())
                    .load(list1.get(position).getUrl())
                    .into(((MyViewHolder) holder).iv);
        }else if(holder instanceof MyViewHolder2){
            ((MyViewHolder2) holder).textView1.setText(list1.get(position).getTitle());
            ((MyViewHolder2) holder).textView2.setText(list1.get(position).getSubtitle());
            Glide.with(context.getApplicationContext())
                    .load(list1.get(position).getUrl())
                    .into(((MyViewHolder2) holder).imageView);

        }
    }

    @Override
    public int getItemCount() {

        return list1.size();
    }

    @Override
    public int getItemViewType(int position) {
        if (list1.get(position).getType()==1){
            return 1;
        }else{
            return 2;
        }
    }
    class MyViewHolder extends RecyclerView.ViewHolder{
        TextView tv2;
        TextView tv;
        ImageView iv;
        public MyViewHolder(final View itemView) {
            super(itemView);
            tv2= (TextView) itemView.findViewById(R.id.category_item_gengduo);
            tv = (TextView) itemView.findViewById(R.id.tv_top);
            iv= (ImageView) itemView.findViewById(R.id.category_item_icon);

            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(onItemClickListener != null){
                        onItemClickListener.onClick(itemView, getAdapterPosition());
                    }
                }
            });


            //设置item的长按事件

            itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    if(onItemClickListener != null){
                        onItemClickListener.onLongClick(itemView, getAdapterPosition());
                    }
                    return true;
                }
            });

        }
    }
    class MyViewHolder2 extends RecyclerView.ViewHolder{
        TextView textView1,textView2;
        ImageView imageView;
        public MyViewHolder2(final View itemView) {
            super(itemView);
            textView1= (TextView) itemView.findViewById(R.id.tv_category_item1);
            textView2= (TextView) itemView.findViewById(R.id.tv_category_item2);
            imageView= (ImageView) itemView.findViewById(R.id.iv_category_item);
            //item点击事件
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(onItemClickListener != null){
                        onItemClickListener.onClick(itemView, getAdapterPosition());
                    }
                }
            });


            //设置item的长按事件

            itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    if(onItemClickListener != null){
                        onItemClickListener.onLongClick(itemView, getAdapterPosition());
                    }
                    return true;
                }
            });

        }
    }
    public interface OnCategoryItemClicklistener{
        void onClick(View itemView, int position);
        void onLongClick(View itemView, int position);
    }
    public void setOnCategoryItemClicklistener(OnCategoryItemClicklistener listener){
        this.onItemClickListener=listener;
    }
}
